package com.dllo.lanou.cookapp.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import com.alibaba.fastjson.JSON;
import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.dllo.lanou.cookapp.R;
import com.dllo.lanou.cookapp.adapter.ScanViewAdapter;
import com.dllo.lanou.cookapp.entity.BookDes;
import com.dllo.lanou.cookapp.entity.BookList;
import com.dllo.lanou.cookapp.entity.BookPage;
import com.dllo.lanou.cookapp.utils.ScanView;
import com.dllo.lanou.cookapp.volly.GetVolleyImage;
import com.dllo.lanou.cookapp.volly.VolleySingleton;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

public class ReadBookActivty extends AppCompatActivity {
    private int id ;
    @Bind(R.id.scanview)
    ScanView scanView;
    private ScanViewAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_read_book_activty);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        id = intent.getIntExtra("id",1);
        requestData();
    }
    //请求数据
    public void requestData() {
        String path = "http://www.tngou.net/api/book/show";
        String httpArg = "id=";
        String url = path +"?"+ httpArg+id;
        Log.i("书籍的的数据详情", url);
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.i("书籍的的数据详情", response);
                BookDes bookDes = JSON.parseObject(response, BookDes.class);
//                BookDes bookDes = gson.fromJson(response,BookDes.class);
                //     List<BookDes> bookDes= gson.fromJson(response.trim(), new TypeToken<List<BookDes>>(){}.getType());//把JSON格式的字符串转为List

                if (bookDes != null){
           List<BookPage>  bookPages =bookDes.getList();
             adapter = new ScanViewAdapter(ReadBookActivty.this,bookPages);
                    scanView.setAdapter(adapter);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }) {
            @Override
            //在StringRespset结束之前写{}在大括号里面写 getHeaders() 自动重写方法
            //用Map方法键值对形势添加
            //添加请求头
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put("apikey", "4ee0087cd4103e3acbab0f8efbf0bbd1");
                return map;
            }


        };
        VolleySingleton.getInstance().addRequest(request);
    }


}
